package com.example.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.domain.Course;
import com.example.result.Result;
import com.example.service.CourseService;
import com.example.tool.GsonTool;

/**
 * @author 袁小弘
 * @describe 课程控制层
 * @date 2019-12-31
 */
@RestController
@RequestMapping("/api")
public class CourseController {

    @Autowired
    CourseService courseService;

    //===============================课程管理==================================================

    /**
     * @param json
     * @return 添加成功则提示成功信息，反之则添加失败
     * @describe 添加课程信息
     */
    @RequestMapping(value = "/courses", method = RequestMethod.POST)
    public Result addCourses(@RequestBody String json) {
        //将JSON数据转换为Course对象实体
        JSONObject parseObject = JSON.parseObject(json);
        Course course = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), Course.class);

        return courseService.addCourse(course);
    }

    /**
     * @param json
     * @return 更新成功则提示成功信息，反之则更新失败
     * @describe 更新课程信息
     */
    @RequestMapping(value = "/courses", method = RequestMethod.PUT)
    public Result updateCourses(@RequestBody String json) {
        //将JSON数据转换为Course对象实体
        JSONObject parseObject = JSON.parseObject(json);
        Course course = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), Course.class);

        return courseService.updateCourse(course);
    }

    /**
     * @param course_Id
     * @return 删除成功则提示成功信息，反之则删除失败
     * @describe 根据课程编号删除该课程信息
     */
    @RequestMapping(value = "/courses/{course_Id}", method = RequestMethod.DELETE)
    public Result deleteCourseById(@PathVariable("course_Id") String course_Id) {

        return courseService.deleteCourseById(course_Id);

    }

    /**
     * @return 以JSON形式返回所有课程信息
     * @describe 查询所有课程信息
     */
    @RequestMapping(value = "/courses", method = RequestMethod.GET)
    public Result queryAllCourses() {
        return courseService.queryAllCourses();

    }

    /**
     * @param course_Id
     * @return 以JSON形式返回该课程信息
     * @describe 根据课程编号查询该课程的信息
     */
    @RequestMapping(value = "/courses/{course_Id}", method = RequestMethod.GET)
    public Result queryCoursesById(@PathVariable("course_Id") String course_Id) {
        return courseService.queryCoursesById(course_Id);

    }

    /**
     * @param json
     * @return 添加成功则返回一个成功提示，反之则提示失败信息
     * @describe 通过Excel批量添加角色信息
     */
    @RequestMapping(value = "/courses/excels", method = RequestMethod.POST)
    public Result addCourseByExcel(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        List<Course> list = JSON.parseArray(parseObject.getString("json"), Course.class);

        System.out.println(list);

        return courseService.addCourseByExcel(list);

    }

    /**
     * @param json
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 课程信息模糊查询
     */
    @RequestMapping(value = "/courses/serachs", method = RequestMethod.POST)
    public Result serachByInfo(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        String[] info = parseObject.getString("json").split(",");

        return courseService.serachByInfo(info);

    }

    /**
     * @param courseIds
     * @return 删除成功则返回一个成功提示，反之则提示失败信息
     * @describe 根据课程编号批量删除课程信息
     */
    @RequestMapping(value = "/courses/dels", method = RequestMethod.POST)
    public Result deleteCourseByIds(@RequestBody Map<String, String> courseIds) {

        return courseService.deleteCourseByIds(courseIds);
    }

    /**
     * @return 以JSON格式返回所有课程信息和课程类型信息
     * @describe 查询所有课程信息和课程类型信息
     */
    @RequestMapping(value = "/courses/all", method = RequestMethod.GET)
    public Result queryAllCourseAndType() {
        return courseService.queryAllCourseAndType();
    }

}
